home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Taifun / Taifun 079 (1988-11-15)(Ossowski, Stefan)(DE)(PD).zip / Taifun 079 (1988-11-15)(Ossowski, Stefan)(DE)(PD).adf / LabyrinthII / input.c < prev    next >
Text File  |  1988-08-14  |  3KB  |  113 lines

  1.  
  2. /* Text input routine by Russell Wallace 1987. Feel free to copy it and use it
  3.  * in your own programs. Requires linkage to my set of console routines,
  4.  * console.o. Backspace and cursor keys can be used. DEL key puts
  5.  * linefeed character in string, RETURN key ends input. Selection of menu
  6.  * item or closewindow gadget will break off input and return -ve number,
  7.  * otherwise length of typed-in string returned. */
  8.  
  9. long checkinput ();
  10.  
  11. long input (string,length)
  12. unsigned char *string;    /* Address of buffer to put text */
  13. long length;            /* Length of buffer */
  14. {
  15.     short c;        /* Key pressed */
  16.     short x;        /* Position in buffer */
  17.     long actlen=0;    /* Actual length of typed-in string */
  18.     register short i;
  19.     resetscroll ();
  20.     x=0;
  21.     for (;;)
  22.     {
  23.         do
  24.             c=checkinput ();
  25.         while (!c);
  26.         if (actlen<x)
  27.             actlen=x;
  28.         if (c==1000)    /* Close window ... cancel edit */
  29.         {
  30.             actlen=-1000;
  31.             goto ENDINPUT;
  32.         }
  33.         if (c<0)        /* Menu select ... cancel edit */
  34.         {
  35.             actlen=c;
  36.             goto ENDINPUT;
  37.         }
  38.         if (c=='\r')    /* Pressed CR ... end edit */
  39.             goto ENDINPUT;
  40.         if (c==8 && x)    /* Backspace key */
  41.         {
  42.             for (i=x-1;i<actlen;i++)    /* Delete character in string buffer */
  43.                 string[i]=string[i+1];
  44.             writechar ((long)8);
  45.             nprint ((long)(string+(--x)));    /* Delete on screen */
  46.             writechar ((long)' ');
  47.             for (i=x;i<actlen;i++)
  48.                 writechar ((long)8);
  49.             actlen--;            /* One fewer characters */
  50.         }
  51.         if (c==155)        /* Code indicating a cursor key etc. */
  52.         {
  53.             do
  54.                 c=checkinput ();    /* Find out which one */
  55.             while (!c);
  56.             if (c==67 && x<actlen)    /* Move right */
  57.             {
  58.                 writechar ((long)155);
  59.                 writechar ((long)67);
  60.                 x++;
  61.             }
  62.             if (c==68 && x)            /* Move left */
  63.             {
  64.                 writechar ((long)155);
  65.                 writechar ((long)68);
  66.                 x--;
  67.             }
  68.             if (c==65 && x>7)        /* Up=back 8 spaces */
  69.             {
  70.                 x-=8;
  71.                 for (i=0;i<8;i++)
  72.                 {
  73.                     writechar ((long)155);
  74.                     writechar ((long)68);
  75.                 }
  76.             }
  77.             if (c==66 && x<actlen-7)    /* Down=forward 8 */
  78.             {
  79.                 x+=8;
  80.                 for (i=0;i<8;i++)
  81.                 {
  82.                     writechar ((long)155);
  83.                     writechar ((long)67);
  84.                 }
  85.             }
  86.             c=0;
  87.         }
  88.         if (c>31 && c<128 && actlen<length-1)
  89.         {
  90.             for (i=actlen;i>x;i--)
  91.                 string[i]=string[i-1];
  92.             string[++actlen]='\0';
  93.             string[x++]=c;
  94.             nprint ((long)(string+x-1));
  95.             for (i=x;i<actlen;i++)
  96.                 writechar ((long)8);
  97.         }
  98.     }
  99. ENDINPUT:
  100.     for (i=0;i<x;i++)
  101.         if (string[i]==127)        /* Convert peculiar characters back into */
  102.             string[i]='\n';            /* CRs for use */
  103.     string[actlen]='\0';        /* Stick null on end of string */
  104.     for (i=x;i<actlen;i++)
  105.     {
  106.         writechar ((long)155);    /* Move cursor to end of string */
  107.         writechar ((long)67);
  108.     }
  109.     writechar ((long)'\n');        /* Print out CR */
  110.     resetscroll ();        /* This has introduced a pause sufficient for any */
  111.     return (actlen);                /* preceding text to have been read */
  112. }
  113.